#include "KcTexTextNode.h"
#include "KuLex.h"
#include "KuStrUtil.h"
#include "../KvPaint.h"


KcTexTextNode::KcTexTextNode(const std::string_view& text, bool addWhitespace, bool stripInnerWhitepace, KvTexNode* parent)
    : super_(parent)
{
    if (stripInnerWhitepace) {
        for (int i = 0; i < text.size(); i++) {
            if (!KuLex::isSpace(text[i])) text_ += text[i];
        }
    }
    else {
        text_ = text;
    }

    // strip all whitespace from left
    while (text_.size() > 1 && KuLex::isSpace(text_[0])) {
        text_ = text_.substr(0, text.size() - 1);
    }

    if ( addWhitespace && !text_.empty() && !KuLex::isSpace(text_.back()) )
        text_ = text_ + " ";

    //qDebug()<<"JKQTMathTextTextNode( text="<<text<<" addWhitespace="<<addWhitespace<<")   [=> this->text="<<this->text<<"]";
}


std::string KcTexTextNode::getTypeName() const
{
    return std::string("TexTextNode(") + text_ + ")";
}


KcTexTextNode::KpSize KcTexTextNode::getSize(KvPaint& paint) const
{
    return getSizeNavie_(paint, text_);
}


double KcTexTextNode::draw(KvPaint& paint, double x, double y) const
{
    super_::draw(paint, x, y);

    auto sz = getSize(paint);
    paint.drawText({ x, y - sz.baselineHeight, 0 }, text_);
    assert(paint.inScreenCoord());
    return x + sz.width;
}
